import 'dart:async';

import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:webview_flutter/webview_flutter.dart';

class CustomerServicePage extends StatefulWidget {
  @override
  _CustomerServiceState createState() => _CustomerServiceState();
}

class _CustomerServiceState extends State<CustomerServicePage> {
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  bool isLoading = true; // 设置状态

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ThemeColors.colorWhite,
      appBar: _initAppBar(),
      body: _initBody(),
    );
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(
      context,
      Text("在线客服", style: FonStyle.navTextStyle()),
      backgroundColor: ThemeColors.colorWhite,
    );
  }

  Widget _initBody() {
    return Stack(
      children: [
        WebView(
          initialUrl: ApiPath.url_customer_service,
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (WebViewController webViewController) {
            _controller.complete(webViewController);
          },
          navigationDelegate: (NavigationRequest request) {
            var url = request.url;
            print("visit $url");
            setState(() {
              isLoading = true; // 开始访问页面，更新状态
            });

            return NavigationDecision.navigate;
          },
          onPageFinished: (String url) {
            setState(() {
              isLoading = false; // 页面加载完成，更新状态
            });
          },
        ),
        isLoading
            ? Container(
                child: Center(
                  child: CircularProgressIndicator(),
                ),
              )
            : Container(),
      ],
    );
  }
}
